<?php

namespace haibao\erp\business\product;

use \haibao\erp\data\product\Product as ProductData;
use \haibao\erp\data\brand\BrandDefine as BrandDefineData; 
use \haibao\erp\business\BusinessException;
use haibao\erp\data\product\ProductContentInfo;
use \haibao\erp\data\product\ProductRelCategoryProperty as ProductRelCategoryPropertyData;
use \haibao\erp\model\filter\product\ProductRelCategoryProperty as ProductRelCategoryPropertyFilter;
use \haibao\erp\data\product\ProductContentInfo as ProductContentInfoData;
use haibao\frame\data\query\Condition;
use \haibao\erp\model\data\product\ProductContentInfo as ProductContentInfoModel;

class Product extends \haibao\erp\business\Base{
	
	/**
	 * 通过单品ID得到单品实体
	 * @param  $productId
	 */
	public static function getProductInfoById($productId){
		if(empty($productId)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$productData = new ProductData();
		$data = $productData->getOneById(intval($productId));
        $productContentInfoData = new ProductContentInfoData();
        $contentInfo = $productContentInfoData->getProductContentInfoByProductIds($productId);

        if(isset($contentInfo[$productId])){
            $data->ProductContentInfo = ProductContentInfoModel::initProductContentInfo();
            $data->ProductContentInfo = $contentInfo[$productId] + $data->ProductContentInfo;
            foreach($data->ProductContentInfo as $item=>$value){
                if($item == \haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_PRICE){
                    if (!empty($value)){
                        $list=array();
                        foreach (json_decode($value,true) as $key=>$v){
                            $list[$key]=$v;
                        }
                        $data->ProductContentInfo[$item] = $list;
                    }

                }elseif ($item == \haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_IMAGE_PRO){
                    if (!empty($value)){

                        $data->ProductContentInfo[$item]=self::getInfobyImgeIdArr($value);
                    }

                }elseif ($item == \haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_CATEGORY_INFO){
                    if (!empty($value)) {
                        $data->ProductContentInfo[$item] = explode('#', $value);
                    }
                }
                elseif ($item == \haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_PROPERTY_MAP_MISAKI_PRODUCT_ID){
                    if (!empty($value)) {
                        $data->ProductContentInfo[$item] = json_decode($value,true);
                    }
                }


                elseif ($item == \haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_DETAIL){
                    if (!empty($value)) {
                        $p = '';
                        if(!is_null(json_decode($value))){
                            foreach (json_decode($value,true) as $key => $v){
                                $p .= $v['DetailInfo'].'<br>';
                            }
                            $data->ProductContentInfo[$item] = $p;
                        }

                    }
                }

            }
        }

        if($data->BrandId){
            $brandDefineData = new BrandDefineData();
            $data->BrandModels = $brandDefineData->getOneById($data->BrandId);
        }


        $productRelCategoryPropertyData = new ProductRelCategoryPropertyData();
		$productRelCategoryPropertyFilter = new ProductRelCategoryPropertyFilter();
		$productRelCategoryPropertyFilter->select(array(
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_ID,
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_PRODUCTID,
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_PROPERTYID,
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_PROPERTYNAME,
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAEM_PROPERTYVALUE,
		    ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAEM_VALUE_TYPE,
		));
		$productRelCategoryPropertyFilter->order(ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_ID,false);
		$productRelCategoryPropertyFilter->where(ProductRelCategoryPropertyFilter::CONDITION_FIELD_NAME_PRODUCTID, Condition::CONDITION_EQUAL, intval($productId));
		
		$data->CategoryProperty = $productRelCategoryPropertyData->getAll($productRelCategoryPropertyFilter);

		return $data;
	}
	
	/**
	 * 通过单品Id取单品信息
	 * @param string $idStr
	 */
	public static function getProductInfoByIds($idStr){
		$productData = new ProductData();
		return $productData->getProductInfoByIds($idStr);
	}
	
	public static function addProduct($model,$propertyVaAr=array()){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		self::checkProductModel($model);
		$productData = new ProductData();
		$productData->addProduct($model,$propertyVaAr);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(true,true));
	}
	
	public static function updateProduct($model,$propertyVaAr){
		self::checkOperation(self::FUNC_CODE_PRODUCT_EDIT);
		self::checkProductModel($model);
		$productData = new ProductData();
		$productData->updateProduct($model,$propertyVaAr);
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_EDIT, $model->toArray(true,true));
	}
	
	private static function checkProductModel($model){
	    if(empty($model)){
	        throw new BusinessException('单品不能为空',self::DATA_EMPTY);
	    }
	    if(empty($model->ProductName)){
	        throw new BusinessException('单品名称ProductName不能为空',self::DATA_EMPTY_ID);
	    }
	    if(empty($model->Title)){
	        throw new BusinessException('单品标题Title不能为空',self::DATA_EMPTY_ID);
	    }
	    /*
	     *数据表更改不检查product表的CategoryId;
	     */
	    /*if(empty($model->CategoryId)){
	        throw new BusinessException('单品所属分类CategoryId不能为空',self::DATA_EMPTY_ID);
	    }*/
	}
	
	public static function updateProductStatus($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_PRODUCT_DEL);
		if(empty($id)){
			throw new BusinessException('单品ID不能为空',self::DATA_EMPTY_ID);
		}
		$productData = new ProductData();
		$productData->updateById(intval($id), $arrSet);
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_DEL, $arrSet);
	}
	
	public static function getProduct($productFilter){
		$productData = new ProductData();
		return $productData->getAll($productFilter);
	}
	
	public static function getOneProduct($productFilter){
		$productData = new ProductData();
		return $productData->getOne($productFilter);
	}

	public static function addProductBySpider($model){

        self::checkProductModel($model);
        $productData = new ProductData();
        $productData->addProductBySpider($model);
        self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(true,true),4);
    }

    public static function checkProductIsExist($originUrl,$categoryId){

        $productData = new ProductData();
        return $productData->checkProductIsExist($originUrl,intval($categoryId));

    }

    public  static function  getProductById($productIds,$dataType=0){
        $productInfoArr = array();
        if(empty($productIds)) return $productInfoArr;
        $product= new ProductData();
        return $product->getProductByProductIds($productIds,$dataType);

    }
    public  static  function  getProductbyCateId($filter){
        $data= new ProductData();
            return $data->getAll($filter);
    }
    public  static  function  addProductContentInfo($model){
        $productData = new ProductData();
        $productData->addProduct($model);
        self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(true,true));
    }
    public  static  function getInfobyImgeIdArr($imgArr){
        if (empty($imgArr)) return array();;
        $ProductContentInfoData= new ProductContentInfoData();
        return $ProductContentInfoData->getImgeInfobyIdArr($imgArr);

    }

    public static function checkProductHasImage($productId,$imageId){
        $productData = new ProductData();
        return $productData->checkProductHasImage(intval($productId),intval($imageId));

    }

    public static function checkProductHasMapMisaki($misakiProductId){
        $productData = new ProductData();
        return $productData->checkProductHasMapMisaki(intval($misakiProductId));

    }
    public  static function prdocutForPrevAndNext($productId,$CategoryId){
        $productData = new ProductData();

        return $productData->prdocutForPrevAndNext($productId,$CategoryId);
    }

}